package asu.util
{
	import flash.utils.Dictionary;

	public class TypeUtil
	{
		public static var integerAttrTypes	:Array = ['x','y','width','height','fontSize','left','right','top','bottom','maxChars'];
		public static var booleanAttrTypes	:Array = ['editable'];
		public static var colorAttrTypes		:Array = ['color'];
		
		public static function isIntegerAttrType( type:String ):Boolean
		{
			return integerAttrTypes.indexOf( type ) > -1;
		}
		
		public static function isBooleanAttrType( type:String ):Boolean
		{
			return booleanAttrTypes.indexOf( type ) > -1;
		}
		
		public static function isColorAttrType( type:String ):Boolean
		{
			return colorAttrTypes.indexOf( type ) > -1;
		}
		
		public static function stringToColor( string:String ):uint
		{
			return uint( string.replace("0x","").replace("#","") );
		}
		
		public static function stringToBoolean( string:String ):Boolean
		{
			if( !string ) return false;
			return ( string.toLowerCase() == 'true' || string == '0' );
		}
		
		public static function toArray( value:*, delimiter:String=" ", length:int=0, extract:Boolean=false ):Array
		{
			var array:Array = [];
			if( value is Array ) array = value;
			if( value is String ) array = (value as String ).split( delimiter );
			
			if( value is Dictionary )
			{
				for( var key:* in value )
				{
					array.push( value[key] );
				}
				
				return array;
			}
			
			if( array.length == 0 ) return array;
			
			if( length > array.length )
			{
				var ol:int = array.length;
				
				while( array.length < length )
				{
					if( extract ) 
					{
						var n:int = array.length;
						
						if( array[ n-ol ] )
						{
							array.push( array[ n-ol ] );
						}
						else if( array[ n-1 ] )
						{
							array.push( array[ n-1 ] );
						}
						else
						{
							array.push( null );
						}
					}
					else
					{
						array.push( null );
					}
				}
			}
			else if( length > 0 && length < array.length )
			{
				array.splice( -(array.length-length), (array.length-length) );
			}
			
			return array;
		}
		
		public static function toBoolean( value:* ):Boolean
		{
			if( value is Boolean ) return value;
			if( value is String ) return ( value as String ).toLowerCase() == 'true' || value == '1';
			if( !isNaN( value ) ) return value > 0;
			return ( value != null ) ? true : false;
		}
		
		public static function numberToHexString( number:Number ):String
		{
			return "0x"+number.toString( 16 ).toUpperCase();
		}
		
		public static function stringToHex( string:String ):uint 
		{
			string = string.replace('#','');
			
			if( string.substring( 0, 2 ) != '0x' )
			{
				string = '0x'+string;
			}
			
			return uint( string );
		}
		
		public static function toHexArray( value:* ):Array
		{
			var values:Array = toArray( value );
			
			for( var i:String in values )
			{
				values[i] = stringToHex( values[i] );
			}
			
			return values;
		}
		
		public static function toHexStringArray( value:* ):Array
		{
			var values:Array = toArray( value );
			
			for( var i:String in values )
			{
				values[i] = numberToHexString( Number( values[i] ) );
			}
			
			return values;
		}
		
		public static function toNumberArray( value:*, delimiter:String=" ", length:int=-1, extract:Boolean=false ):Array
		{
			var values:Array = toArray( value, delimiter, length, extract );
			
			//trace( values, delimiter, length, extract );
			
			for( var i:String in values )
			{
				values[i] = Number( values[i] );
			}
			
			return values;
		}
	}
}